﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HuynhQuangWebsite.Models;
namespace HuynhQuangWebsite.Controllers
{
    public class ManageOnlineSupportController : BaseController
    {
        huynhquangco_websiteEntities ctx = new huynhquangco_websiteEntities();
        //
        // GET: /ManageAbout/

        public ActionResult OnlineSupport()
        {
            var os = (from aboutus in ctx.OnlineSupports where aboutus.OSID != 0 select aboutus).SingleOrDefault();
            if (os != null)
                return View(os);
            else
            {
                os = new Models.OnlineSupport();
                return View(os);
            }
        }

        [HttpPost]
        public ActionResult UpdateToOnlineSupport([Bind(Exclude = "OSID")]OnlineSupport os ,int id)
        {
            try
            {
                //if (!IsAuthenticateAdmin())
                //    return RedirectToAction("LogOnDashBoard", "DashBoard");
                EnCrypto cript = new EnCrypto();
                var currentOS= (from aboutus in ctx.OnlineSupports where aboutus.OSID == id select aboutus).SingleOrDefault();
                if (currentOS != null)
                {
                   
                    UpdateModel(currentOS);
                    currentOS.OSPwdEmailFrom = cript.Encrypt(currentOS.OSPwdEmailFrom);
                    ctx.SaveChanges();
                    return RedirectToAction("OnlineSupport");
                }
                else
                {
                   
                    os.OSPwdEmailFrom = cript.Encrypt(os.OSPwdEmailFrom);
                    ctx.AddToOnlineSupports(os);
                    ctx.SaveChanges();
                    return RedirectToAction("OnlineSupport");
                }
            }
            catch
            {
                return View();
            }
        }
    }
}
